/**********************************************************************************************************************
 *
 * Project:    mGameEngine
 * File:       lock.cpp
 * Author:     karooolek
 * Created on: 2009-02-27
 *
 **********************************************************************************************************************/

#include "lock.h"

namespace mGameEngine
{

Lock::Lock(bool locked)
{
#ifdef WIN32
    InitializeCriticalSection(&_mutex);
#else
    pthread_mutex_init(&_mutex, NULL);
#endif

    // initial lock
    if(locked)
    {
        lock();
    }
}

Lock::~Lock()
{
#ifdef WIN32
    DeleteCriticalSection(&_mutex);
#else
    pthread_mutex_destroy(&_mutex);
#endif
}

void Lock::lock()
{
#ifdef WIN32
    EnterCriticalSection(&_mutex);
#else
    pthread_mutex_lock(&_mutex);
#endif
}

bool Lock::tryLock()
{
#ifdef WIN32
    return TryEnterCriticalSection(&_mutex) == TRUE;
#else
    return pthread_mutex_trylock(&_mutex) == 0;
#endif
}

void Lock::unlock()
{
#ifdef WIN32
    LeaveCriticalSection(&_mutex);
#else
    pthread_mutex_unlock(&_mutex);
#endif
}

}

